//
//  OperationBillChooseCarVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/11/19.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "OperationBillChooseCarVC.h"

@interface OperationBillChooseCarVC ()<UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <OperationBillChooseCarModel *>*array;

@end

@implementation OperationBillChooseCarVC

-(NSMutableArray <OperationBillChooseCarModel *>*)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - BottomHeight - NaviHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)requestForList {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(page) forKey:@"page"];
    [HttpRequest PostWithApi:LogisticsSupplierTaskDiaoDuCarList params:dic progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [OperationBillChooseCarModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [OperationBillChooseCarModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择车辆";
    page = 1;
    
    [self.view addSubview:self.tableView];
    [self requestForList];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 1)];
    line.backgroundColor = Color_backgroundColor;
    [self.view addSubview:line];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"OperationBillChooseCarCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 16, ScreenW - 30, 18)];
    titleLab.textColor = HexColor(0x222222);
    titleLab.font = [UIFont systemFontOfSize:17];
    titleLab.text = self.array[indexPath.row].num;
    [cell.contentView addSubview:titleLab];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 49, ScreenW, 1)];
    line.backgroundColor = Color_backgroundColor;
    [cell.contentView addSubview:line];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.finishBlock) {
        self.finishBlock(self.array[indexPath.row].vehicle_id, self.array[indexPath.row].num);
    }
    [self.rt_navigationController popViewControllerAnimated:YES];
}

@end

@implementation OperationBillChooseCarModel

+(NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"Id":@"id"};
}

@end
